package com.regex.web.baidu.ai.image;

import java.net.URLEncoder;

import com.regex.web.baidu.ai.AuthService;
import com.regex.web.baidu.ai.Base64Util;
import com.regex.web.baidu.ai.FileUtil;
import com.regex.web.baidu.ai.HttpUtil;
import com.regex.web.baidu.ai.NetImageUtils;

/**
 * 银行卡识别 
 * @author ocean.regex
 *
 */
public class Bankcard {
	public static void main(String[] args) {
        // 银行卡识别url
//        String bankcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard";
        // 本地图片路径
//        String filePath = "C:/Users/admin/Desktop/pic/bank1.jpg";
//        try {
//            byte[] imgData = FileUtil.readFileByBytes(filePath);
//            String imgStr = Base64Util.encode(imgData);
//            String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(imgStr, "UTF-8");
//            /**
//             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
//             */
//            String accessToken = AuthService.getAuth();
//            String result = HttpUtil.post(bankcardIdentificate, accessToken, params);
//            System.out.println(result);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
		String filePath = "C:/Users/admin/Desktop/pic/bank1.jpg";
		localPicBankcardDist(filePath);
    }
	/**
	 * 本地图片
	 * @param filePath
	 * @return
	 */
	public static String localPicBankcardDist(String filePath) {
		// 银行卡识别url
        String bankcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard";
        // 本地图片路径
        String result = "";
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(imgStr, "UTF-8");
            /**
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
             */
            String accessToken = AuthService.getAuth();
            result = HttpUtil.post(bankcardIdentificate, accessToken, params);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
	}
	/**
	 * 网络图片
	 * @param netPicPath
	 * @return
	 */
	public static String netPicBankcardDist(String netPicPath) {
		// 银行卡识别url
        String bankcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard";
        // 本地图片路径
        String result = "";
        try {
            byte[] imgData = NetImageUtils.getImgerFormNetByUrl(netPicPath);
            String imgStr = Base64Util.encode(imgData);
            String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(imgStr, "UTF-8");
            /**
             * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
             */
            String accessToken = AuthService.getAuth();
            result = HttpUtil.post(bankcardIdentificate, accessToken, params);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
	}
}
